$(function() {
	var $loginform = $("#login_form");
	$loginform.get(0).reset();
	var $regform = $loginform.next();
	$regform.get(0).reset();
	var $username = $("#username");
	var $password = $("#password");
	$username.validate({
		type: "regex",
		data: "email",
		error: "请输入正确的邮箱"
	}).iPrompt({
		text:"用户名"
	});
	$password.validate({
		type : "regex",
		data : ".{6,}",
		error:"请输入密码，至少为6位"
	}).iPrompt({
		text:"密码"
	});
	var $regemail = $("#regemail");
	var emailsrv = $regemail.attr("srv");
	$regemail.iPrompt({
		text : "请输入您的邮箱地址"
	});
	$regform.submit(function() {
		var email = $.trim($regemail.val());
		if(!$.getReg('email').test(email)) {
			$.showError($regemail , "请输入正确的邮箱");
			return false;
		} else {
			var submit = $(this).find(":submit").get(0);
			submit.disabled = true;
			var result;
			$.ajax({
				data:{
					email : email
				},
				type : "POST",
				url : emailsrv,
				dataType : "json",
				async : false,
				success : function(d) {
					submit.disabled = false;
					var code = d.code;
					if(code) {
						$.showError($regemail , "对不起，您输入的邮箱已注册，请更换其他邮箱，或直接登录");
						result = false;
					} else {
						result = true;
					}
				},
				error : function() {
					$.showError($regemail , "对不起，服务器通讯失败，请稍后再试");
					submit.disabled = false;
					result = false;
				}
			});
			return result;
		}
	});
	$.subscribe('click_show_reg' , __reg , false)
	 .subscribe('click_hide_reg' , __reg , false);
	function __reg(t,e,action) {
		if(action == 'click_show_reg') {
			$regform.find(">div:first").show();
		} else {
			$regform.find(">div:first").hide();
		}
	}
	$("#peoplelist").usercard();
});